clear all
set more off

use "gravity_model_trade_costs_merged.dta", clear

replace gdpcap_d = exp(ln_GDP_per_capita_d)/1000 if missing(gdpcap_d)
replace gdpcap_o = exp(6.008813)/1000 if exporter_num == 130
replace gdpcap_o = exp(7.62949)/1000 if exporter_num == 139
gen lngdpcap_d = ln(gdpcap_d*1000)
gen lngdpcap_o = ln(gdpcap_o*1000)
gen ln_ag_trade_cost = ln(ag_trade_cost-1)
gen ln_manu_trade_cost = ln(manu_trade_cost-1)

gen domestic_consumption = 0
replace domestic_consumption = 1 if iso3_o == iso3_d 


***** ELIMINATING TARIFFS AND ENTERING FREE TRADE AGREEMENTS *****
preserve 

* Run regression in Equation 24 for agriculture

reg ln_ag_trade_cost contig distw_arithmetic_jh cost_import_d days_import_d ///
fta_wto tariff_primary_simple_2011_d ///
comlang_off comcol col45 if domestic_consumption == 0

* Eliminate tariffs, enforce free trade agreements everywhere
replace tariff_primary_simple_2011_d = 0
replace fta_wto = 1

* Predict trade costs under policy hypothetical 
predict ln_ag_trade_cost_no_tariffs_fta
gen ag_trade_cost_no_tariffs_fta = exp(ln_ag_trade_cost_no_tariffs_fta) + 1

* Don't allow policy reform prediction to increase trade costs
replace ag_trade_cost_no_tariffs_fta = ag_trade_cost ///
if ag_trade_cost < ag_trade_cost_no_tariffs_fta

* Don't allow predicted trade costs to go below 1
replace ag_trade_cost_no_tariffs_fta = 1 if ag_trade_cost < 1

save "ag_trade_costs_no_tariffs_FTA.dta", replace 

keep exporter_num importer_num ag_trade_cost_no_tariffs_fta

sort exporter_num importer_num 
reshape wide ag_trade_cost_no_tariffs_fta, i(exporter_num) j(importer_num)
drop exporter_num 
export delimited "ag_trade_costs_no_tariffs_FTA.csv", replace 

restore 

* Repeat above for manufacturing 

preserve 

reg ln_manu_trade_cost contig distw_arithmetic_jh cost_import_d days_import_d ///
fta_wto tariff_manu_simple_2011_d ///
comlang_off comcol col45 if domestic_consumption == 0

replace tariff_manu_simple_2011_d = 0
replace fta_wto = 1

predict ln_manu_trade_cost_no_tar_fta
gen manu_trade_cost_no_tar_fta = exp(ln_manu_trade_cost_no_tar_fta) + 1

replace manu_trade_cost_no_tar_fta = manu_trade_cost ///
if manu_trade_cost < manu_trade_cost_no_tar_fta
replace manu_trade_cost_no_tar_fta = 1 if manu_trade_cost < 1

save "manu_trade_costs_no_tariffs_FTA.dta", replace 

keep exporter_num importer_num manu_trade_cost_no_tar_fta

sort exporter_num importer_num 
reshape wide manu_trade_cost_no_tar_fta, i(exporter_num) j(importer_num)
drop exporter_num 
export delimited "manu_trade_costs_no_tariffs_FTA.csv", replace 
restore 
*/


***** ELIMINATING ALL POLICY BARRIERS *****

* Same procedure as above, but now also reduce "red tape barriers" to the global frontier level
* In particular, set days_import to 3 and cost_import to 300 when predicting new trade cost matrix

preserve

reg ln_ag_trade_cost contig distw_arithmetic_jh cost_import_d days_import_d ///
fta_wto tariff_primary_simple_2011_d ///
comlang_off comcol col45 if domestic_consumption == 0

replace tariff_primary_simple_2011_d = 0
replace fta_wto = 1
replace days_import = 3
replace cost_import = 300 if cost_import > 300 

predict ln_ag_trade_cost_no_red_tape
gen ag_trade_cost_no_red_tape = exp(ln_ag_trade_cost_no_red_tape) + 1

replace ag_trade_cost_no_red_tape = ag_trade_cost ///
if ag_trade_cost < ag_trade_cost_no_red_tape

replace ag_trade_cost_no_red_tape = 1 if ag_trade_cost < 1

save "/Users/ishannath/Dropbox/Climate & Global Comparative Advantage/Data/Gravity_dta_V202211/ag_trade_costs_no_policy_barriers.dta", replace 

keep exporter_num importer_num ag_trade_cost_no_red_tape

sort exporter_num importer_num 
reshape wide ag_trade_cost_no_red_tape, i(exporter_num) j(importer_num)
drop exporter_num 
export delimited "/Users/ishannath/Dropbox/Climate & Global Comparative Advantage/Data/Gravity_dta_V202211/ag_trade_costs_no_policy_barriers.csv", replace 
restore 

preserve 

reg ln_manu_trade_cost contig distw_arithmetic_jh cost_import_d days_import_d ///
fta_wto tariff_manu_simple_2011_d ///
comlang_off comcol col45 if domestic_consumption == 0

replace tariff_manu_simple_2011_d = 0
replace fta_wto = 1
replace days_import = 3
replace cost_import = 300 if cost_import > 300 

predict ln_manu_trade_cost_no_red_tape
gen manu_trade_cost_no_red_tape = exp(ln_manu_trade_cost_no_red_tape) + 1

replace manu_trade_cost_no_red_tape = manu_trade_cost ///
if manu_trade_cost < manu_trade_cost_no_red_tape
replace manu_trade_cost_no_red_tape = 1 if manu_trade_cost < 1

save "/Users/ishannath/Dropbox/Climate & Global Comparative Advantage/Data/Gravity_dta_V202211/manu_trade_costs_no_policy_barriers.dta", replace 

keep exporter_num importer_num manu_trade_cost_no_red_tape

sort exporter_num importer_num 
reshape wide manu_trade_cost_no_red_tape, i(exporter_num) j(importer_num)
drop exporter_num 
export delimited "/Users/ishannath/Dropbox/Climate & Global Comparative Advantage/Data/Gravity_dta_V202211/manu_trade_costs_no_policy_barriers.csv", replace 
restore 



***** SAVE REGRESSION RESULTS FOR A TABLE *****

*/
reg ln_ag_trade_cost contig distw_arithmetic_jh cost_import_d days_import_d ///
fta_wto tariff_primary_simple_2011_d ///
comlang_off comcol col45 if domestic_consumption == 0

estimates store ag_trade_costs_regression

reg ln_manu_trade_cost contig distw_arithmetic_jh cost_import_d days_import_d ///
fta_wto tariff_manu_simple_2011_d ///
comlang_off comcol col45 if domestic_consumption == 0

estimates store manu_trade_costs_regression

esttab ag_trade_costs_regression manu_trade_costs_regression using "trade_cost_decomposition.tex", ///
nostar label booktabs width(0.9\hsize) replace */

* Alternative version of the trade cost regression that only includes the exogenous variables in Equation 24

reg ln_ag_trade_cost contig distw_arithmetic_jh ///
comlang_off comcol col45 if domestic_consumption == 0

estimates store ag_trade_costs_reg_2

reg ln_manu_trade_cost contig distw_arithmetic_jh ///
comlang_off comcol col45 if domestic_consumption == 0

estimates store manu_trade_costs_reg_2

esttab ag_trade_costs_regression ag_trade_costs_reg_2 manu_trade_costs_regression manu_trade_costs_reg_2 using "trade_cost_decomposition_exogenous_only.tex", ///
nostar label booktabs width(0.9\hsize) replace 
